package com.ch.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ch.entity.User;
import com.ch.config.RedisUtil;
import com.ch.dao.UserDao;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    private static Logger log=Logger.getLogger(UserService.class);
    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisUtil redisUtil;
    public User login(String userName, String pwd){
        User user=null;
        String key=userName+"_"+pwd;
        user= (User) redisUtil.get(key);
        if (user==null){
            log.info("redis没有对应的数据，你提供的数据是：username:"+userName+"  ;pwd:"+pwd);
            QueryWrapper<User> userQueryWrapper=new QueryWrapper();
            userQueryWrapper.eq("user_name",userName)
                    .eq("pwd",pwd);
            user= userDao.selectOne(userQueryWrapper);
            if (user!=null){
                log.info(("数据从数据库里面获得，写到了redis，数据是："+user.toString()));
                redisUtil.set(key,user);
            }
        }
        else {
            log.info("这个数据从redis里面获得，数据是："+user.toString());
        }
        return user;
    }
    public boolean insert(User user){
        log.info(user.toString());
        boolean isSuccess=false;
        int insert = userDao.insert(user);
        if (insert>0){
            isSuccess=true;
        }
        return isSuccess;
    }
    public User getbyid(int id){
        //这个地方使用mongdb更好。先mysql数据库用吧，后期有空再来改
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("id",id);
        return  userDao.selectOne(userQueryWrapper);
    }
}
